package Tree;


import java.util.ArrayList;
import java.util.List;

/**
 * 先序遍历树
 */
public class PreorderTraversal {
    public static void main(String[] args) {

        TreeNode left1 = new TreeNode(4);
        TreeNode left2 = new TreeNode(5);
        TreeNode right1 = new TreeNode(6);

        TreeNode left = new TreeNode(2,left1,left2);
        TreeNode right = new TreeNode(3,null,right1);
        TreeNode node = new TreeNode(1,left,right);

        List<Integer> list = solution(node);
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }

    public static List<Integer> solution(TreeNode node){
        List<Integer> res = new ArrayList<>();
        preorder(node,res);
        return res;
    }

    public static  void preorder(TreeNode node,List<Integer> res){
        if(node == null) return;
        res.add(node.val);
        preorder(node.left,res);
        preorder(node.right,res);

    }
}
